<template>
  <div class="menu-container">
    <el-menu
        :default-active="defaultActive"
        active-text-color="#ffd04b"
        background-color="#2c3e50"
        class="my-menu"
        text-color="#fff"
        router
        collapse-transition
    >
      <el-sub-menu
          v-for="book in books"
          :key="book.id"
          :index="`/book/${book.id}`"
          class="menu-item"
      >
        <template #title>
          <el-icon class="menu-icon"><Location /></el-icon>
          <span>{{ book.name }}</span>
        </template>
        <el-menu-item
            v-for="section in book.sectionList"
            :key="section.id"
            :index="`/book/${book.id}/section/${section.id}`"
            class="menu-item"
        >
          {{ section.name }}
        </el-menu-item>
      </el-sub-menu>
    </el-menu>
  </div>
</template>

<script lang="ts" setup>
import { reactive, ref, onMounted } from 'vue';
import request from '../utils/request';
import { Location } from '@element-plus/icons-vue'; // 引入图标

// 定义数据类型
interface Section {
  id: number;
  name: string;
}

interface Book {
  id: number;
  name: string;
  sectionList: Section[];
}

const books = reactive<Book[]>([]); // 存储书籍和章节数据
const defaultActive = ref<string>('1'); // 动态设置默认激活项

// 获取书本数据
onMounted(async () => {
  try {
    const { data } = await request.get('/api/user/book/list');
    books.push(...data.data);
    // 设置默认选中的菜单项（如果需要根据某个逻辑进行动态设置）
    if (books.length > 0) {
      defaultActive.value = `/book/${books[0].id}`; // 默认选择第一个书本
    }
  } catch (error) {
    console.error('获取书本数据失败:', error);
  }
});
</script>

<style scoped>
/* 父容器设置：菜单容器样式 */
.menu-container {
  height: 100vh;
  display: flex;
  flex-direction: column;
  justify-content: flex-start;
  background-color: #34495e; /* 菜单的背景色 */
  padding: 20px 10px;
  overflow: hidden; /* 隐藏滚动条 */
}

/* 菜单容器样式 */
.my-menu {
  height: 100%;
  overflow-y: auto;
}

/* 隐藏滚动条 */
.my-menu::-webkit-scrollbar {
  display: none;
}

/* 基本样式 */
.el-menu {
  width: 100%;
  padding: 0;
  background-color: #34495e; /* 菜单背景色 */
}

.el-sub-menu {
  background-color: #34495e;
  margin-bottom: 10px;
  border-radius: 8px;
  overflow: hidden;
}

/* 子菜单项样式 */
.el-menu-item {
  padding: 12px 20px;
  font-size: 14px;
  border-radius: 5px;
  transition: background-color 0.3s ease;
}

/* 菜单项悬停效果 */
.el-menu-item:hover {
  background-color: #3e566e;
}

/* 动画过渡：子菜单项的菜单图标和文本对齐 */
.menu-item {
  transition: background-color 0.3s ease, padding 0.3s ease;
}

.menu-item .el-menu-item__inner {
  display: flex;
  align-items: center;
}

/* 图标的样式 */
.menu-icon {
  margin-right: 10px;
  color: #ffd04b; /* 图标颜色 */
}

/* 高亮显示选中的菜单项 */
.el-menu-item.is-active {
  background-color: #2c3e50;
  color: #ffd04b;
  border-radius: 5px;
  font-weight: bold;
}

/* 设置激活的子菜单标题 */
.el-sub-menu.is-active .el-sub-menu__title {
  background-color: #2c3e50;
  color: #ffd04b;
}

/* 子菜单标题的悬停效果 */
.el-sub-menu__title:hover {
  background-color: #3e566e;
  cursor: pointer;
}

/* 确保菜单项的过渡效果流畅 */
.el-menu-item {
  transition: all 0.3s ease;
}

</style>
